package booking.utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.moomi.common.model.BkBooking;
import com.moomi.common.model.BkItem;
import com.moomi.common.model.BkShop;
import com.utils.DateUtils;
import com.utils.Utils;

public class BookingUtils extends BKUtils {

	public BookingUtils(int shopId) {
		super(shopId);
	}

	/**
	 * 是否允许屏蔽个别时段
	 */
	private static final boolean shouldBreak = false;

	/**
	 * 帮客户自动选择可以预约时间
	 */
	private static final boolean shouldAutoSelectAvailableTime = true;

	public static final boolean hideTimeOutBookingInfo = false;

	public static final boolean hideUnavalibelBooking = true;

	public boolean shouldAutoSelectAvailableTime() {
		return shouldAutoSelectAvailableTime;
	}

	/**
	 * 是否在营业时间
	 * 
	 * @param time
	 * @param shop
	 * @return
	 */
	private boolean shouldShowTime(String time, String bookDay, BkShop shop) {
		Date date = new Date();
		String dt = DateUtils.dateFormat(date, "yyyy-MM-dd");
		Date open = DateUtils.dateFromString(bookDay + " " + shop.getOpenStr(), "yyyy-MM-dd HH:mm");
		Date close = DateUtils.dateFromString(bookDay + " " + shop.getCloseStr(), "yyyy-MM-dd HH:mm");
		Date selectedTime = DateUtils.dateFromString(bookDay + " " + time, "yyyy-MM-dd HH:mm");
		if (selectedTime.getTime() <= date.getTime()) {
			return false;
		}
		boolean flag = false;
		if (open.getTime() <= selectedTime.getTime() && selectedTime.getTime() <= close.getTime()) {
			flag = true;
		}
		return flag;
	}

	public ArrayList<HashMap<String, Object>> getTimeArray(Integer currentTime, int selectionId, int itemId,
			String bookDay) {
		int time = currentTime == null ? -1 : currentTime;
		ArrayList<Boolean> spareTimeArray = getSpareTime(selectionId, itemId, bookDay);
		String string = "";
		int i = 0;
		ArrayList<HashMap<String, Object>> arrayList = new ArrayList<HashMap<String, Object>>();
		boolean timeSelected = time == -1 ? false : true;
		timeSelected = timeSelected || shouldAutoSelectAvailableTime;
		// System.out.println("timeSelected: " + timeSelected);
		for (Boolean boolean1 : spareTimeArray) {
			if (time == -1) {
				if (boolean1) {
					time = i;
				}
			}
			int hour = i / timeScale;
			int minute = i % timeScale * (60 / timeScale);
			String timeFmt = hour + ":" + ((minute == 0) ? "00" : minute);
			if (hour < 10) {
				timeFmt = "0" + timeFmt;
			}
			string = string + timeFmt + "\t: " + boolean1 + "\n";
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("time", timeFmt);
			map.put("flag", boolean1);
			map.put("selected", (i == time) && timeSelected);
			map.put("index", i);

			/**
			 * 是否应该显示出当前时间
			 */
			// boolean shouldShowTime = (hour >= shop.getOpenTime() && hour <
			// shop.getCloseTime());
			boolean shouldShowTime = shouldShowTime(timeFmt, bookDay, shop);
			if (BookingUtils.shouldBreak) {
				int breakStart = 12 * timeScale;
				int breakEnd = 14 * timeScale;
				boolean shouldBreak = (i >= breakStart && i < breakEnd);
				if (shouldBreak) {
					shouldShowTime = false;
				}
			}
			// System.out.println(string + " shouldShowTime: " +
			// shouldShowTime);
			if (shouldShowTime) {
				if (BookingUtils.hideUnavalibelBooking) {
					boolean flag = (boolean) map.get("flag");
					if (flag) {
						arrayList.add(map);
					}
				} else {
					arrayList.add(map);
				}
			}

			i++;
		}
		return arrayList;
	}

	public ArrayList<Boolean> getSpareTime2(int selectionId, int itemId, String bookDay) {
		return null;
	}

	private void checkTimes(ArrayList<Boolean> times) {
		// for (int i = 0; i < times.size(); i++) {
		// System.out.println(timeUtil.timeIndexToString(i) + ": " +
		// times.get(i));
		// }
	}

	public ArrayList<Boolean> getSpareTime(int selectionId, int itemId, String bookDay) {
		if (itemId == -1) {
			return new ArrayList<>();
		}

		BkItem item = BkItem.dao.findById(itemId);

		List<BkBooking> booking = BkBooking.dao.getBooking(selectionId, bookDay);
		ArrayList<Boolean> array = new ArrayList<Boolean>();

		for (int i = 0; i < timePiece; i++) {
			array.add(true);
		}

		// System.out.println(bookDay);
		// System.out.println("yyyy-MM-dd:" + Utils.dateNow("yyyy-MM-dd"));
		if (bookDay.equals(Utils.dateNow("yyyy-MM-dd"))) {
			int timeIndexNow = timeUtil.timeIndexNow();
			for (int i = 0; i < timePiece; i++) {
				if (i <= timeIndexNow) {
					array.set(i, false);
				}
			}
		}
		array = businessHours(shop.getOpenTime(), shop.getCloseTime(), array);
		// 如果允许无限预约,则不判断是否有充足时间
		if (item.getLimit() == 0) {
			return array;
		}
		int tmpTimeScale = timeScale;
		tmpTimeScale = 1;
		// 计算未被预约时间
		float spend = item.getSpend() * tmpTimeScale;

		for (BkBooking bkBooking : booking) {
			int time = bkBooking.getBookTime();
			float itemSpend = bkBooking.getFloat("spend") * tmpTimeScale;
			float endTime = time + itemSpend;
			for (int i = time; i < endTime; i++) {
				array.set(i, false);
			}
		}
		// 计算当前项目是否有充足时间
		for (int i = 0; i < timePiece; i++) {
			if (array.get(i) == true) {
				int startTime = i * 1;
				float endTime = i * 1 + spend;
				for (int j = startTime; j < endTime; j++) {
					String ttt = timeUtil.timeIndexToString(i);
					int endHour = j / timeScale;
					int endMinute = (j % timeScale) * (60 / timeScale);
					String eee = endHour + ":" + endMinute + " ";
					String timeFmt = ttt + "- " + eee;
					if (j >= timePiece) {
						array.set(i, false);
						continue;
					} else {
						boolean flag = array.get(j);
						if (flag == false) {
							array.set(i, false);
							continue;
						}
					}
				}
			}
		}
		return array;
	}

	public ArrayList<Boolean> businessHours(int open, int close, ArrayList<Boolean> array) {

		if (shouldBreak) {
			int breakStart = 12 * timeScale;
			int breakEnd = 14 * timeScale;
			for (int i = breakStart; i < breakEnd; i++) {
				array.set(i, false);
			}
		}

		for (int i = 0; i < open * timeScale; i++) {
			array.set(i, false);
		}

		for (int i = close * timeScale; i < 24 * timeScale; i++) {
			array.set(i, false);
		}

		return array;
	}

	public ArrayList<String> selectionDates() {
		ArrayList<String> dates = new ArrayList<String>();
		for (int i = 0; i < 15; i++) {
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.DATE, i);
			String day = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
			dates.add(day);
		}
		return dates;
	}

}
